package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.BrandRemoteConfigConstant;
import com.tbit.uqbike.constant.UserConstants;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.business.AccountUserFreeLoginService;
import com.tbit.uqbike.service.business.BrandRemoteConfigService;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.RequestContext;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * 品牌对接参数配置Controller
 *
 * @author jing
 * @date 2021-04-21
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/brandremoteconfig")
public class BrandRemoteConfigController extends BaseController {
    @Autowired
    private BrandRemoteConfigService brandRemoteConfigService;
    @Autowired
    private AccountUserFreeLoginService freeLoginService;

    /**
     * 查询品牌对接参数配置列表
     */
    @GetMapping("/list")
    public JsonResult list(BrandRemoteConfig brandRemoteConfig) {
        PageInfo<BrandRemoteConfig> pageInfo = brandRemoteConfigService.selectBrandRemoteConfigList(brandRemoteConfig);
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal(),null);
    }

    /**
     * 根据品牌id查询密钥参数
     *
     * @param brandId 品牌id
     */
    @GetMapping("/getOperaPswEnable")
    public JsonResult getOperaPswEnable(@NotNull(message = "ver.accountId.notNull") Integer brandId) {
        BrandRemoteConfig brandRemoteConfig = brandRemoteConfigService.getRemoteConfigByBrandIdAndConfigType(brandId, BrandRemoteConfigConstant.OPERA_PSW);
        if (brandRemoteConfig == null){
            BrandRemoteConfig brandRemoteConfigDefault = new BrandRemoteConfig();
            brandRemoteConfigDefault.setAccountId(brandId);
            brandRemoteConfigDefault.setConfigType(BrandRemoteConfigConstant.OPERA_PSW);
            brandRemoteConfigDefault.setParamKV("1");
            brandRemoteConfigService.insertBrandRemoteConfig(brandRemoteConfigDefault);
            brandRemoteConfig = brandRemoteConfigDefault;
        }
        return JsonResult.ok(Objects.equals(brandRemoteConfig.getParamKV(), "1"));
    }

    /**
     * 新增品牌对接参数配置
     */
    @PostMapping
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.BRAND_CONFIG_ADD)
    public JsonResult add(@Validated @RequestBody BrandRemoteConfig brandRemoteConfig) {
        RequestContext context = getLogin(ServletUtils.getRequest()).context;
        if (UserConstants.NOT_UNIQUE.equals(brandRemoteConfigService.checkConfigTypeUnique(brandRemoteConfig))) {
            return JsonResult.fail(context.getMessage("ver.acocunt.configType.exists"), TraceUtil.getTraceIdByLocal());
        }
        return JsonResult.toAjax(brandRemoteConfigService.insertBrandRemoteConfig(brandRemoteConfig), context);
    }

    /**
     * 新增多个品牌对接参数配置
     */
    @PostMapping("/addBrandConfig")
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.BRAND_CONFIG_ADD)
    public JsonResult addBrandConfig(@Validated @RequestBody String data) {
        LoginInfo login = getLogin(ServletUtils.getRequest());
        RequestContext context = login.context;
        List<BrandRemoteConfig> brandRemoteConfigList = JSONArray.parseArray(JSONObject.parseObject(data).getString("data"), BrandRemoteConfig.class);
        int res= brandRemoteConfigService.addBrandRemoteConfig(brandRemoteConfigList);
        return JsonResult.ok();
    }

    /**
     * 修改品牌对接参数配置
     */
    @PutMapping
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.BRAND_CONFIG_EDIT)
    public JsonResult edit(@Validated @RequestBody BrandRemoteConfig brandRemoteConfig) {
        return JsonResult.toAjax(brandRemoteConfigService.updateBrandRemoteConfig(brandRemoteConfig), getLogin(ServletUtils.getRequest()).context);
    }

    /**
     * 删除品牌对接参数配置
     */
    @DeleteMapping
    @OperateLog(module = ModuleEnum.ACCOUNT, methods = MethodEnum.BRAND_CONFIG_DEL)
    public JsonResult remove(@Validated @RequestBody BrandRemoteConfig brandRemoteConfig) {
        return JsonResult.toAjax(brandRemoteConfigService.deleteBrandRemoteConfigById(brandRemoteConfig), getLogin(ServletUtils.getRequest()).context);
    }

    /**
     * 获取api管理平台地址
     *
     * @param brandId
     * @return
     */
    @RequestMapping("/getTbitConfig")
    public JsonResult getTbitConfig(Integer brandId) {
        String url = brandRemoteConfigService.getTbitConfig(brandId);
        return JsonResult.ok(url);
    }
}
